package com.aisile.sellergoods.service.impl;

import com.aisile.entity.PageResult;
import com.aisile.mapper.TbSpecificationMapper;
import com.aisile.mapper.TbSpecificationOptionMapper;
import com.aisile.pojo.TbSpecification;
import com.aisile.pojo.TbSpecificationExample;
import com.aisile.pojo.TbSpecificationOption;
import com.aisile.pojo.TbSpecificationOptionExample;
import com.aisile.pojogroup.Specifacation;
import com.aisile.sellergoos.service.SpecificationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Override
    public PageResult findPage(int page, int rows, TbSpecification tbSpecification) {
        PageHelper.startPage(page,rows);
        String specName = tbSpecification.getSpecName();
        TbSpecificationExample example=new TbSpecificationExample();
        if(!StringUtils.isEmpty(specName)){
            example.createCriteria().andSpecNameLike("%"+specName+"%");
        }
        Page<TbSpecification> pageModel=(Page<TbSpecification>) tbSpecificationMapper.selectByExample(example);
        return new PageResult(pageModel.getTotal(),pageModel.getResult());
    }


    @Override
    public void save(Specifacation specifacation) {
        TbSpecification spec = specifacation.getTbSpecification();//规格主表数据
        //规格 选项列表数据
        List<TbSpecificationOption> specOptionList = specifacation.getTbSpecificationOptions();
        //保存主表信息
        tbSpecificationMapper.insert(spec);
        //保存选项信息
        if(specOptionList!=null && specOptionList.size()>0){
            for(TbSpecificationOption option : specOptionList){
                option.setSpecId(spec.getId());
                tbSpecificationOptionMapper.insert(option);
            }
        }
    }

    @Override
    public Specifacation findOne(long id) {
        //根据id查询出主表信息
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        //根据id查询出 规格对应的选项数据
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        //封装到组合实体
        return new Specifacation(tbSpecification,tbSpecificationOptions);
    }

    @Override
    public void update(Specifacation specifacation) {
        TbSpecification tbSpecification = specifacation.getTbSpecification();//规格主表数据
        //规格 选项列表数据
        List<TbSpecificationOption> tbSpecificationOptions = specifacation.getTbSpecificationOptions();

        //1.更新tbSpecification信息
        tbSpecificationMapper.updateByPrimaryKey(tbSpecification);
        //2.根据specId 进行条件删除
        TbSpecificationOptionExample example=new TbSpecificationOptionExample();
        example.createCriteria().andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        //3.再添加规格选项集合
        if (tbSpecificationOptions!=null && tbSpecificationOptions.size()>0){
            for (TbSpecificationOption tbSpecificationOption:tbSpecificationOptions){
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insert(tbSpecificationOption);
            }
        }
    }

    @Override
    public void del(Long[] ids) {
        //根据ID值循环删除主表
        for(long id : ids){
            tbSpecificationMapper.deleteByPrimaryKey(id);

            //循环删除选项表
            TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
            tbSpecificationOptionExample.createCriteria().andSpecIdEqualTo(id);
            tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);

        }

    }

}
